package com.xzit.dao.impl;

import com.xzit.dao.TeamDao;
import com.xzit.model.Team;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Repository
public class TeamDaoImpl implements TeamDao {
    @Autowired
    private DataSource dataSource;
    @Override
    public void addTeam(Team team) {
        String sql = "insert into team(name, address) values(?,?)";
        try {
            Connection coon = dataSource.getConnection();
            PreparedStatement ps = coon.prepareStatement(sql);
            ps.setString(1, team.getName());
            ps.setString(2, team.getAddress());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteTeamByName(String name) {
        String sql = "DELETE FROM team WHERE name=?";
        try (Connection conn = dataSource.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, name);
            ps.executeUpdate();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
